package com.qt;

import com.qt.entity.Author;

import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
import java.util.function.Consumer;
import java.util.function.Function;
import java.util.function.Predicate;
import java.util.function.Supplier;

public class MethodDemo01 {

    interface UseString{
        String use(String str,int start,int length);
    }

    public static void main(String[] args) {
        test05();
        //test04();
        //test03();
        //test02();
        //test01();
    }

    private static void test05() {

        String res = subAuthorName("it大牛", new UseString() {
            @Override
            public String use(String s, int beginIndex, int endIndex) {
                return s.substring(beginIndex, endIndex);
            }
        });

        String result = subAuthorName("it大牛", String::substring);


    }

    public static String subAuthorName(String str, UseString useString){
        int start = 0;
        int length = 1;
        return useString.use(str,start,length);
    }

    private static void test04() {
        /**
         *
         */
//        getAuthorOptional().ifPresent(
//                authors -> authors.stream()
//                        .map(author -> author.getAge())
//                        .map(String::valueOf).forEach(age -> System.out.println(age))
//        );

        StringBuilder sb = new StringBuilder();
        getAuthorOptional().ifPresent(
                authors -> authors.stream()
                        .map(author -> author.getName())
                        .forEach(sb::append)
        );
    }

    private static void test03() {
        /** negate
         * 打印作家中年龄不大于17的作家。
         */
        Optional<List<Author>> authorOptional = getAuthorOptional();
        authorOptional.ifPresent(
                authors -> authors.stream().filter(((Predicate<Author>) author -> author.getAge() > 17).negate()
        ).forEach(author -> System.out.println(author.getName()))
        );


    }

    private static void test02() {
        /** or
         * 打印作家中年龄大于17或者姓名的长度小于2的作家。
         */
        Optional<List<Author>> authorOptional = getAuthorOptional();
        authorOptional.ifPresent(authors -> authors.stream()
                .filter(new Predicate<Author>() {
                    @Override
                    public boolean test(Author author) {
                        return author.getAge() > 17;
                    }
                }.or(new Predicate<Author>() {
                    @Override
                    public boolean test(Author author) {
                        return author.getName().length() < 2;
                    }
                })
                ).forEach(author -> System.out.println(author.getName()))
        );

    }

    private static void test01() {

        /** and
         * 打印作家中年龄大于17并且姓名的长度大于1的作家。
         */
        Optional<List<Author>> authorOptional = getAuthorOptional();
        authorOptional.ifPresent(
                authors -> authors.stream()
                        .filter(((Predicate<Author>) author -> author.getAge() > 17)
                .and(author -> author.getName().length() > 1))
                        .forEach(author -> System.out.println(author.getName()))
        );


    }

    private static Optional<List<Author>> getAuthorOptional() {

        Author author1 = new Author(1L, "懵躲", 23, "狂狼一条该", null);
        Author author2 = new Author(2L, "诺手", 13, "为了自由而战", null);
        List<Author> list = new ArrayList<>();
        list.add(author1);
        list.add(author2);
        return Optional.ofNullable(list);

    }


}
